{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       col2      col1\n",
      "1  0.979614 -0.305169\n",
      "6 -0.646504 -0.122038\n",
      "4  0.113034  0.458910\n",
      "2 -0.614051  0.524252\n",
      "3 -1.219582  0.746265\n",
      "5 -0.378610 -0.940556\n",
      "9 -1.264779 -3.409872\n",
      "8 -2.709128 -0.692051\n",
      "0 -1.182564  0.775077\n",
      "7 -0.062695  0.104726\n"
     ]
    }
   ],
   "source": [
    "import pandas as pd\n",
    "import numpy as np\n",
    "\n",
    "unsorted_df=pd.DataFrame(np.random.randn(10,2),index=[1,6,4,2,3,5,9,8,0,7],columns=['col2','col1'])\n",
    "print(unsorted_df)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       col2      col1\n",
      "0 -1.182564  0.775077\n",
      "1  0.979614 -0.305169\n",
      "2 -0.614051  0.524252\n",
      "3 -1.219582  0.746265\n",
      "4  0.113034  0.458910\n",
      "5 -0.378610 -0.940556\n",
      "6 -0.646504 -0.122038\n",
      "7 -0.062695  0.104726\n",
      "8 -2.709128 -0.692051\n",
      "9 -1.264779 -3.409872\n"
     ]
    }
   ],
   "source": [
    "# 按照列排序\n",
    "print(unsorted_df.sort_index())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       col1      col2\n",
      "1 -0.305169  0.979614\n",
      "6 -0.122038 -0.646504\n",
      "4  0.458910  0.113034\n",
      "2  0.524252 -0.614051\n",
      "3  0.746265 -1.219582\n",
      "5 -0.940556 -0.378610\n",
      "9 -3.409872 -1.264779\n",
      "8 -0.692051 -2.709128\n",
      "0  0.775077 -1.182564\n",
      "7  0.104726 -0.062695\n"
     ]
    }
   ],
   "source": [
    "# 按照列排序\n",
    "print(unsorted_df.sort_index(axis=1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "       col2      col1\n",
      "9 -1.264779 -3.409872\n",
      "5 -0.378610 -0.940556\n",
      "8 -2.709128 -0.692051\n",
      "1  0.979614 -0.305169\n",
      "6 -0.646504 -0.122038\n",
      "7 -0.062695  0.104726\n",
      "4  0.113034  0.458910\n",
      "2 -0.614051  0.524252\n",
      "3 -1.219582  0.746265\n",
      "0 -1.182564  0.775077\n"
     ]
    }
   ],
   "source": [
    "# 按照值排序\n",
    "print(unsorted_df.sort_values(by='col1'))"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
